
/*
 * Copyright 2008 Friendster Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
*/

using System;
using System.Collections;
using System.Text;
using System.IO;


namespace Friendster.Service.Core
{
    /// <summary>
    /// Represents response gets from Friendster.
    /// </summary>
    public sealed class FriendsterResponse
    {
        // Fields
        private string _message;
        private string _rawResponse;
        private object _result;
        private int _status;

        // Methods
        /// <summary>
        /// Constructor. This method also sends response string to XML reader to parse the response.
        /// </summary>
        /// <param name="responseStream"></param>
        internal FriendsterResponse(Stream responseStream)
        {
            this._rawResponse = new StreamReader(responseStream).ReadToEnd();
            try
            {
                object obj2 = new XMLReader(this._rawResponse).ReadValue();
                Hashtable hashtable = obj2 as Hashtable;
                if (hashtable != null)
                {
                    object obj3 = hashtable["error_code"];
                    if (obj3 != null)
                    {
                        this._status = System.Convert.ToInt32(obj3);
                        this._message = (string)hashtable["error_msg"];
                    }
                }
                if (this._status==0)
                {
                    this._result = obj2;
                }
            }
            catch(Exception e)
            {
                throw new InvalidOperationException(e.ToString());
            }

        }

        // Properties
        /// <summary>
        /// Provides the error message involved with current response, if any.
        /// </summary>
        public string ErrorMessage 
        {
            get
            {
                return this._message;
            }
        }
        /// <summary>
        /// returns a boolean value indicating whether there is an error or not.
        /// </summary>
        public bool IsError 
        {
            get
            {
                return (this._status!=0);
            }
        }
        /// <summary>
        /// Raw response in string format.
        /// </summary>
        public string RawResponse 
        {
            get
            {
                return this._rawResponse;
            }
        }
        /// <summary>
        /// Raw Response as object
        /// </summary>
        public object Result 
        {
            get
            {
                return this._result;
            }
        }
    }
}
